package weaver.jobs.afilm.duss.utils;

import cn.hutool.crypto.digest.DigestAlgorithm;
import cn.hutool.crypto.digest.Digester;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import okhttp3.*;
import weaver.conn.RecordSet;
import weaver.general.Util;

/**
 * 每刻报销人员，部门批量同步
 *
 * @author dusongsong
 * @date 2023年06月29日 09:47
 */
public class MaycurUtil {

    /**
     * 获取分部ID
     *
     * @param departid
     * @return
     */
    public static String getsubcompanyid1(String departid) {
        RecordSet rs = new RecordSet();
        rs.executeQuery("select subcompanyid1 from hrmdepartment where id=?", departid);
        rs.next();
        return Util.null2String(rs.getString(1));
    }

    /**
     * 获取部门级别
     *
     * @return
     */
    public static String getTlevel() {
        String tlevels = "";
        RecordSet rs = new RecordSet();
        rs.executeQuery("select tlevel from hrmdepartment group by tlevel order by tlevel");
        while (rs.next()) {
            String tlevel = Util.null2String(rs.getString(1));
            tlevels += "".equals(tlevels) ? tlevel : "," + tlevel;
        }
        return tlevels;
    }

    /**
     * 获取token
     *
     * @return
     */
    public static JSONObject getToken() {
        String resultStr = "";
        JSONObject paramsJson = new JSONObject();
        // appCode 和appSecret请联系每刻工作人员
        // String appCode = "AP3RRI7V8BH9AP";
        // String appSecret = "ixVTcToV3bShWDHm5Kwl";
        String appCode = "AP3SGXBF5SIYPZ";
        String appSecret = "4NvZ53AUMmzX57nInlYsCRl4VU0tLjVs";
        long timestamp = System.currentTimeMillis();
        //String url = "https://ng-uat.maycur.com/api/openapi/auth/login";测试
        String url = "https://ng.maycur.com/api/openapi/auth/login";
        String secret = getSecret(appCode, appSecret, timestamp);
        paramsJson.put("secret", secret);
        paramsJson.put("appCode", appCode);
        paramsJson.put("timestamp", timestamp);
        try {
            OkHttpClient client = new OkHttpClient().newBuilder()
                    .build();
            MediaType mediaType = MediaType.parse("application/json");
            RequestBody body = RequestBody.create(mediaType, paramsJson.toJSONString());
            Request request = new Request.Builder()
                    .url(url)
                    .method("POST", body)
                    .addHeader("Content-Type", "application/json")
                    .build();
            Response response = client.newCall(request).execute();
            resultStr = response.body().string();
            System.out.println(resultStr);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return JSONObject.parseObject(resultStr);
    }

    /**
     * 生成密钥
     *
     * @param appCode
     * @param appSecret
     * @param timestamp
     * @return
     */
    public static String getSecret(String appCode, String appSecret, long timestamp) {
        Digester sha256 = new Digester(DigestAlgorithm.SHA256);
        return sha256.digestHex(appSecret + ":" + appCode + ":" + timestamp);
    }

    /**
     * 批量同步部门接口
     *
     * @param departmentList
     * @param entCode
     * @param tokenId
     * @return
     */
    public static String departmentBatch(JSONArray departmentList, String entCode, String tokenId) {
        //String url = "https://ng-uat.maycur.com/api/openapi/org/department/batch";测试
        String url = "https://ng.maycur.com/api/openapi/org/department/batch";
        return sendOKHttpRequest(departmentList.toJSONString(), url, entCode, tokenId);
    }

    /**
     * 员工批量同步接口
     *
     * @param employeeList
     * @param entCode
     * @param tokenId
     * @return
     */
    public static String employeeBatch(JSONArray employeeList, String entCode, String tokenId) {
        //String url = "https://ng-uat.maycur.com/api/openapi/employee/batch";测试
        String url = "https://ng.maycur.com/api/openapi/employee/batch";
        return sendOKHttpRequest(employeeList.toJSONString(), url, entCode, tokenId);
    }

    /**
     * 获取部门自定义部门主管
     *
     * @param deptid
     * @return
     */
    public static String getDepartDefined(String deptid) {
        RecordSet rs = new RecordSet();
        rs.executeQuery("select bmzg from hrmdepartmentdefined where deptid=?", deptid);
        rs.next();
        return Util.null2String(rs.getString(1));
    }

    /**
     * 获取部门编码
     *
     * @param departid
     * @return
     */
    public static String getDepartmentCode(String departid) {
        RecordSet rs = new RecordSet();
        rs.executeQuery("select departmentcode from hrmdepartment where id=?", departid);
        rs.next();
        return Util.null2String(rs.getString(1));
    }

    /**
     * 获取部门Id
     *
     * @param departmentcode
     * @return
     */
    public static String getDepartmentId(String departmentcode) {
        RecordSet rs = new RecordSet();
        rs.executeQuery("select id from hrmdepartment where departmentcode=?", departmentcode);
        rs.next();
        return Util.null2String(rs.getString(1));
    }

    /**
     * 获取成本中心
     *
     * @param resourceid
     * @return
     */
    public static String getCbzx(String resourceid) {
        RecordSet rs = new RecordSet();
        String sql = "select field2 from cus_fielddata where scope='HrmCustomFieldByInfoType' and scopeid='3' and id=?";
        rs.executeQuery(sql, resourceid);
        rs.next();
        return Util.null2String(rs.getString(1));
    }

    /**
     * 获取员工工号
     *
     * @param resourceid
     * @return
     */
    public static String getWorkCode(String resourceid) {
        RecordSet rs = new RecordSet();
        rs.executeQuery("select workcode from hrmresource where id=?", resourceid);
        rs.next();
        return Util.null2String(rs.getString(1));
    }

    /**
     * 获取员工id
     *
     * @param workcode
     * @return
     */
    public static String getResourceId(String workcode) {
        RecordSet rs = new RecordSet();
        rs.executeQuery("select id from hrmresource where workcode=?", workcode);
        rs.next();
        return Util.null2String(rs.getString(1));
    }

    /**
     * 发送http请求
     *
     * @param params
     * @param url
     * @return
     */
    public static String sendOKHttpRequest(String params, String url, String entCode, String tokenId) {
        String resultStr = "";
        try {
            OkHttpClient client = new OkHttpClient().newBuilder()
                    .build();
            MediaType mediaType = MediaType.parse("application/json");
            RequestBody body = RequestBody.create(mediaType, params);
            Request request = new Request.Builder()
                    .url(url)
                    .method("POST", body)
                    .addHeader("Content-Type", "application/json")
                    .addHeader("entCode", entCode)
                    .addHeader("tokenId", tokenId)
                    .build();
            Response response = client.newCall(request).execute();
            resultStr = response.body().string();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resultStr;
    }
}
